<?php

namespace Home\Controller;

use Think\Controller;

class SchoolController extends Controller
{
    /*定义Tag数组 */
    protected $tag_list = ['1' => '财大气粗', '2' => '人杰地灵', '3' => '下次再来', '4' => '卧虎藏龙', '5' => '人杰辈出'];

    /*学生信息列表*/
    public function index()
    {
        /*查询条件 */
        $map['status'] = 1;
        /*搜序*/
        $search_name = I('search_name');
        if ($search_name) {
            $map['name'] = array('like', '%' . $search_name . '%');
            $this->assign('search_name', $search_name);
        }
        /*调用模型*/
        $School = D('School');
        $list = $School->getPages($map);
        foreach ($list['list'] as $k => $v) {
            if ($v['tag']) {
                /*用逗号, 把字符串分隔成数组*/
                $tag = explode(',', $v['tag']);
                $tag_str = '';
                foreach ($tag as $v2) {
                    $tag_str .= $this->tag_list[$v2] . ',';
                }
                /*去掉结尾逗号*/
                $list['list'][$k]['tag'] = rtrim($tag_str, ',');
            }
            /*性别转换成文字形式
            $list['list'][$k]['sex'] = $v['sex']?'男':'女';*/
            /*时间戳转日期格式*/
            $list['list'][$k]['create_time'] = date('Y-m-d', $v['create_time']);
        }
        $this->assign('list', $list);
        $this->display();
    }

    /*新增/编辑*/
    public function edit()
    {
        /*调用模型*/
        $School = D('School');
        if (IS_POST) {
            /*接收post传过来的所有数据*/
            $data = I('post.');
            /*接收上传的照片文件*/
            $file = $_FILES;
            if (!empty($file['photo']['name'])) {
                $upload = new \Think\Upload();// 实例化上传类
                $upload->maxSize = 3145728;// 设置附件上传大小
                $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
                $upload->rootPath = './Uploads/'; // 设置附件上传根目录
                $upload->savePath = ''; // 设置附件上传（子）目录
                // 上传文件
                $info = $upload->upload();
                if (!$info) {// 上传错误提示错误信息
                    $this->error($upload->getError());
                    exit;
                } else {// 上传成功
                    /*拼接图片地址*/
                    $data['photo'] = ltrim($upload->rootPath, '.') . $info["photo"]['savepath'] . $info["photo"]['savename'];
                }
            }
            if ($data['tag']) {
                /*把数组变成用逗号,分隔的字符串*/
                $data['tag'] = implode(',', $data['tag']);
            }
            $data['create_time'] = time();
            $res = $School->update($data);
            if ($res) {
                /*操作成功 跳转到index*/
                $this->success('操作成功', U('index'));
                exit;
            } else {
                $this->error('操作失败,' . $School->getError());
                exit;
            }
        }
        $id = I('id');
        if ($id) {
            $map['id'] = $id;
            $info = $School->getFind($map);
            $info['tag'] = explode(',', $info['tag']);
            $this->assign('info', $info);
        }
        $this->assign('tag_list', $this->tag_list);
        $this->display();
    }

    /*删除图片*/
    public function ajaxDelImg()
    {
        if (IS_POST) {
            $id = I('id');
            if ($id) {
                $map['id'] = $id;
                $School = D('School');
                $info = $School->getFind($map);
                $img = '.' . $info['photo'];
                /*数据库删除图片地址*/
                $info['photo'] = '';
                $res = $School->update($info);
                if ($res) {
                    /*服务器上删除图片*/
                    unlink($img);
                    $data['status'] = 1;
                    $data['msg'] = '删除成功';
                } else {
                    $data['status'] = 0;
                    $data['msg'] = '删除失败';
                }
            } else {
                $data['status'] = 0;
                $data['msg'] = '参数错误';
            }
        } else {
            $data['status'] = 0;
            $data['msg'] = '非法操作';
        }
        $this->ajaxReturn($data);
    }

    /*删除信息*/
    public function setStatus()
    {
        $ids = I('ids');
        $status = I('status','-1');
        if($ids){
            /*删除条件*/
            $map['id'] = array('in',$ids);
            /*调用模型*/
            $School = D('School');
            $update_data['status'] = $status;
            $res = $School->del($map,$update_data);
            if($res){
                $this->success('删除成功');
            }else{
                $this->error('删除失败');
            }
        }else{
            $this->error('参数错误');
        }
    }
}
